package com.niuzi.util;

import com.niuzi.entity.Admin;
import com.niuzi.entity.Student;
import com.niuzi.entity.Teacher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DataUtil {
    public static List<Admin> admins = new ArrayList<>();
    public static List<Teacher> teacherList = new ArrayList<>();
    public static List<Student> students = new ArrayList<>();
    static {
        Admin admin = new Admin();
        admin.setUsername("admin");
        admin.setPassword("asd");
        admin.setName("小明");
        admin.setSex("男");
        admin.setJiguan("广东省");
        admin.setPowers(Arrays.asList("1", "2", "3"));
        //默认添加一个管理员
        addAdmin(admin);

        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        student.setSex(1);
        addStudent(student);

        Student student2 = new Student();
        student2.setName("小红");
        student2.setAge(18);
        student2.setSex(0);
        addStudent(student2);

        Student student3 = new Student();
        student3.setName("小刚");
        student3.setAge(20);
        student3.setSex(1);
        addStudent(student3);
    }
    public static void addAdmin(Admin admin){
        admins.add(admin);
    }

    public static Admin queryAdmin(String username , String password){
        for (Admin admin : admins) {
            if (admin.getUsername().equals(username) && admin.getPassword().equals(password)){
                return admin;
            }
        }
        return null;
    }

    public static  void addStudent(Student student){
        students.add(student);

    }
    public static List<Student> queryStudent(){
        return new ArrayList<>(students);
    }
    public  static  boolean deleteStudent(String name){
        for (Student student: new ArrayList<>(students)){
            if (student.getName().equals(name)){
                students.remove(student);
                return true;
            }
        }
        return false;
    }

    public static void addTeacher(Teacher teacher){
        teacherList.add(teacher);
    }
    public static List<Teacher> queryTeacher(){

       return new ArrayList<>(teacherList);

    }
    public static void delTeacher(String thname){
        for (Teacher teacher : new ArrayList<>(teacherList)) {
            if(teacher.getThname().equals(thname)){
                 teacherList.remove(teacher);
            }
        }
    }

}
